local wezterm = require("wezterm")
local config = wezterm.config_builder()

config.automatically_reload_config = true

local colors_init = require("colors.init")
local colors = colors_init["flexoki"]

-- config.colors = colors

config.webgpu_power_preference = "HighPerformance"
config.webgpu_preferred_adapter = wezterm.gui.enumerate_gpus()[1]

-- config.color_scheme = "Nord (Gogh)"
-- config.color_scheme = "tokyonight_moon"
config.color_scheme = "GruvboxDark"
-- config.color_scheme = "Gruvbox Material (Gogh)"

-- 设置字体和窗口大小
config.font = wezterm.font("JetBrainsMono Nerd Font Mono")
config.font_size = 12.5
config.audible_bell = "Disabled"
config.default_cursor_style = "BlinkingBlock"
config.window_close_confirmation = "NeverPrompt"
config.window_decorations = "RESIZE"
config.enable_tab_bar = false
config.window_background_opacity = 0.92
config.macos_window_background_blur = 20
config.initial_cols = 168
config.initial_rows = 48
config.tab_max_width = 25
config.show_tab_index_in_tab_bar = true
config.window_padding = {
	left = 10,
	right = 10,
	top = 7,
	bottom = 7,
}

config.background = {
	-- {
	--
	-- 	source = {
	-- 		File = "/home/loong/Pictures/Screenshots/bg1.png",
	-- 	},
	-- 	hsb = {
	-- 		hue = 1.0,
	-- 		saturation = 1.02,
	-- 		brightness = 0.25,
	-- 	},
	-- 	width = "100%",
	-- 	height = "100%",
	-- },
	-- {
	-- source = {
	-- 	Color = "#282c35",
	-- },
	-- 	source = { Color = colors.background },
	-- 	width = "100%",
	-- 	height = "100%",
	-- 	opacity = 0.92,
	-- },
}

-- 键盘绑定
local act = wezterm.action

config.leader = { key = ";", mods = "CTRL", timeout_milliseconds = 1500 }
config.keys = {
	{ key = "q", mods = "LEADER", action = act.QuitApplication },

	{ key = "h", mods = "LEADER", action = act.SplitHorizontal({ domain = "CurrentPaneDomain" }) },
	{ key = "v", mods = "LEADER", action = act.SplitVertical({ domain = "CurrentPaneDomain" }) },
	{ key = "c", mods = "LEADER", action = act.CloseCurrentPane({ confirm = false }) },

	{ key = "h", mods = "CTRL|SHIFT", action = act.ActivatePaneDirection("Left") },
	{ key = "l", mods = "CTRL|SHIFT", action = act.ActivatePaneDirection("Right") },
	{ key = "k", mods = "CTRL|SHIFT", action = act.ActivatePaneDirection("Up") },
	{ key = "j", mods = "CTRL|SHIFT", action = act.ActivatePaneDirection("Down") },

	-- CTRL + T 创建默认的Tab
	{ key = "t", mods = "LEADER", action = act.SpawnTab("DefaultDomain") },
	-- CTRL + W 关闭当前Tab
	{ key = "w", mods = "LEADER", action = act.CloseCurrentTab({ confirm = false }) },
}

for i = 1, 8 do
	-- ALT + number to activate that tab
	table.insert(config.keys, {
		key = tostring(i),
		mods = "LEADER",
		action = act.ActivateTab(i - 1),
	})
end

-- 鼠标绑定
config.mouse_bindings = {
	{
		-- right clink select (not wezterm copy mode),copy, and if don't select anything, paste
		-- https://github.com/wez/wezterm/discussions/3541#discussioncomment-5633570
		event = { Down = { streak = 1, button = "Right" } },
		mods = "NONE",
		action = wezterm.action_callback(function(window, pane)
			local has_selection = window:get_selection_text_for_pane(pane) ~= ""
			if has_selection then
				window:perform_action(act.CopyTo("ClipboardAndPrimarySelection"), pane)
				window:perform_action(act.ClearSelection, pane)
			else
				window:perform_action(
					act({
						PasteFrom = "Clipboard",
					}),
					pane
				)
			end
		end),
	},
	-- Change the default click behavior so that it only selects
	-- text and doesn't open hyperlinks
	{
		event = { Up = { streak = 1, button = "Left" } },
		mods = "NONE",
		-- action = act.CompleteSelection('ClipboardAndPrimarySelection'),
		action = act.Nop,
	},
	-- and make CTRL-Click open hyperlinks
	{
		event = { Up = { streak = 1, button = "Left" } },
		mods = "CTRL",
		action = act.OpenLinkAtMouseCursor,
	},
}

return config
